package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 订单结算（VIP套餐、专辑、声音三种商品结算）
     *
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "订单结算（VIP套餐、专辑、声音三种商品结算）")
    @PostMapping("/orderInfo/trade")
    public Result<OrderInfoVo> trade(@RequestBody TradeVo tradeVo) {
        Long userId = AuthContextHolder.getUserId();
        OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo, userId);
        return Result.ok(orderInfoVo);
    }


    /**
     * 提交订单（付款方式：1.余额 2.微信）
     *
     * @param orderInfoVo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "提交订单（付款方式：1.余额 2.微信）")
    @PostMapping("/orderInfo/submitOrder")
    public Result<Map<String, String>> submitOrder(@RequestBody OrderInfoVo orderInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        Map<String, String> result = orderInfoService.submitOrder(orderInfoVo, userId);
        return Result.ok(result);
    }


    /**
     * 查询订单及订单明细+优惠列表
     *
     * @param orderNo
     * @return
     */
    @Operation(summary = "查询订单及订单明细+优惠列表")
    @GetMapping("/orderInfo/getOrderInfo/{orderNo}")
    public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
        return Result.ok(orderInfo);
    }


    /**
     * 分页获取当前用户订单列表
     *
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "分页获取当前用户订单列表")
    @GetMapping("/orderInfo/findUserPage/{page}/{limit}")
    public Result<Page<OrderInfo>> findUserOrderPage(@PathVariable Long page, @PathVariable Long limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<OrderInfo> pageInfo = new Page<>(page, limit);
        pageInfo = orderInfoService.findUserOrderPage(pageInfo, userId);
        return Result.ok(pageInfo);
    }


    /**
     * 用户支付成功后，更新订单状态以及完成权益发放
     *
     * @param orderNo
     * @return
     */
    @Operation(summary = "用户支付成功后，更新订单状态以及完成权益发放")
    @GetMapping("/orderInfo/orderPaySuccess/{orderNo}")
    public Result orderPaySuccess(@PathVariable String orderNo) {
        orderInfoService.orderPaySuccess(orderNo);
        return Result.ok();
    }

}

